<?php
/**
 * User: Nicho
 * Date: 17-3-31
 * Time: AM 08:10
 */

namespace Admin\Controller;


class MerchantController extends AdminController
{
    //单位表
    private $merchantModel;
    //单位配置表
    private $merchantConfModel;
    //省市区表
    private $districtModel;
    public function __construct(){
        parent::__construct();
        $this->merchantModel = M('core_merchant');
        $this->districtModel = M('district');
        $this->merchantConfModel = M('core_merchant_config');
        $this->merchantConfModel->create();
        $this->merchantModel->create();
    }
    const MODEL_NAME = 'Merchant@:';

    //商户列表
    public function index()
    {
        if(I('keyword')){
            $keyword = I('keyword');
            $map['title'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        $map['status'] = array('neq',-1);
        $totalCount = $this->merchantModel->where($map)->count();
        $page = new \Think\Page($totalCount,20);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $this->merchantModel->where($map)->limit($limit)->order('id desc')->select();
        
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '商户列表';
        $this->display(self::MODEL_NAME.'Merchant/index'); 
        
    }
    //添加/编辑单位
    public function updateMerchant()
    {
        if(IS_POST){
            $this->merchantModel->provinceID = I('provinceID');
            $this->merchantModel->cityID = I('cityID');
            $this->merchantModel->areaID = I('areaID');
            switch (I('a_type')) {
                case 'add':
                    $this->merchantModel->status = 1;
                    $this->merchantModel->create_time = time();
                    $result = $this->merchantModel->add();
                    break;
                case 'edit':
                    $this->merchantModel->update_time = time();
                    $result = $this->merchantModel->where('id = '.I('id'))->save();
                    break;
            }
            if($result){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            $Merchant = $this->merchantModel->where('id = '.I('id'))->find();
            $this->assign('data',$Merchant);
            $this->assign('a_type',I('a_type'));
            $this->assign('pro',AddonsController::pro());
            $this->display(self::MODEL_NAME.'Merchant/updateMerchant');
        }
    }
    //设置状态
     public function setStatus()
    {
        $ids = I('request.id');
        $model = I('request.model');
        if(!$ids)$this->error('无可操作数据!');
        $map['id'] = array('in',$ids);
        $method = I('request.method');
        $model_object = D($model);
        switch ($method) {
            case 'resume':
                $model_object->status = 1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('激活成功!');
                }else{
                    $this->error('激活失败!');
                }
                break;
            case 'forbid':
                $model_object->status = 2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('禁用成功!');
                }else{
                    $this->error('禁用失败!');
                }
                break;
            case 'delete':
                $model_object->status = -1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('删除成功!');
                }else{
                    $this->error('删除失败!');
                }
                break;
            case 'recover':
                $model_object->status = 0;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('恢复成功!');
                }else{
                    $this->error('恢复失败!');
                }
                break;
            case 'destory':
                $model_object->status = -2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('彻底删除成功!');
                }else{
                    $this->error('彻底删除失败!');
                }
                break;
            default:
                $this->error('系统参数错误!');
                break;
        }
    }
}
